/**
 * 变更单
 */
export default {
  data() {
    return {
      contrastClientData: {},
      contrastOrderData: {},
      contrastPublishData: {},
      contrastResourceData: {}
    };
  },
  computed: {
    contrastClientDataKeys() {
      if (!this.contrastClientData) return [];
      return Object.keys(this.contrastClientData);
    },
    contrastOrderDataKeys() {
      if (!this.contrastOrderData) return [];
      const clientKeys = this.contrastOrderData.clientInfo ? Object.keys(this.contrastOrderData.clientInfo) : [];
      const orderKeys = this.contrastOrderData.orderInfo ? Object.keys(this.contrastOrderData.orderInfo) : [];
      return clientKeys.concat(orderKeys);
    },
    contrastPublishDataKeys() {
      if (!this.contrastPublishData) return [];
      return Object.keys(this.contrastPublishData);
    },
    contrastResourceDataKeys() {
      if (!this.contrastResourceData) return [];
      return Object.keys(this.contrastResourceData);
    }
  },
  methods: {
    // 对比数据，判断是否修改了数据
    // 客户信息模块
    contrastClient() {
      if (!this.isChangeOrder) return;
      const backupsData = this.backupsData; // 备份数据
      const params = this.getParams(); // 当前数据
      const contrastKey = ['clientId', 'contactPersonId', 'actualClientId', 'actualContactPersonId']; // 需要对比的字段
      const ans = {};
      contrastKey.forEach(i => {
        if (params[i] !== backupsData[i]) {
          ans[i] = params[i];
        }
      });
      this.contrastClientData = ans;
    },
    // 订单信息模块
    contrastOrder() {
      if (!this.isChangeOrder) return;
      const backupsData = this.backupsData; // 备份数据
      const params = this.getParams(); // 当前数据
      const ans = {};
      const contrastClientKey = ['marketId'];
      if (params.clientInfo && backupsData.clientInfo) {
        ans.clientInfo = {};
        contrastClientKey.forEach(i => {
          if (params.clientInfo[i] !== backupsData.clientInfo[i]) {
            ans.clientInfo[i] = params.clientInfo[i];
          }
        });
      }
      const contrastOrderKey = [
        'attributionMatrix', 'orderTime', 'theme', 'priceBase',
        'orderSource', 'remark', 'content', 'incomeType', 'needCategoryId', 'orderKind',

        'planPrice', 'taxAmount', 'articlesPlanPrice', 'articlesPrice',
        'channelPrice', 'articlesChannelPrice',
        'planDiscountPrice', 'channelDiscountPrice', 'discountPrice'
      ];
      const arrKey = ['cooperationDepartment', 'orderOperator'];

      if (params.orderInfo && backupsData.orderInfo) {
        ans.orderInfo = {};
        contrastOrderKey.forEach(i => {
          if (i !== 'orderKind' && params.orderInfo[i] !== backupsData.orderInfo[i]) {
            ans.orderInfo[i] = params.orderInfo[i];
          }
          // orderKind 是数组格式
          if (i === 'orderKind') {
            // 原来没有值，修改后有值
            if ((params.orderInfo[i] && params.orderInfo[i].length > 0) && (!backupsData.orderInfo[i] || backupsData.orderInfo[i].length === 0)) {
              ans.orderInfo[i] = params.orderInfo[i];
            } else {
              const orderKindIds = params.orderInfo[i].map(j => j.id);
              const backupsOrderKindIds = backupsData.orderInfo[i].map(j => j.id);
              orderKindIds.sort((a, b) => a - b);
              backupsOrderKindIds.sort((a, b) => a - b);
              if (orderKindIds.join(',') !== backupsOrderKindIds.join(',')) {
                ans.orderInfo[i] = params.orderInfo[i];
              }
            }
          }
        });
        arrKey.forEach(i => {
          this.diffArr(ans.orderInfo, params.orderInfo, backupsData.orderInfo, i, 'id');
        });
      }
      this.contrastOrderData = ans;
    },
    contrastResource() {
      if (!this.isChangeOrder) return;
      const backupsData = this.backupsData; // 备份数据
      const params = this.getParams(); // 当前数据
      const contrastKey = [
        'operateMode', 'resourceType', 'orderPlatformId', 'resourceId', 'orderFormId', 'resourceCode', 'systemId'
      ]; // 需要对比的字段
      const ans = {};
      contrastKey.forEach(i => {
        if (params[i] !== backupsData[i]) {
          ans[i] = params[i];
        }
      });
      this.contrastResourceData = ans;
    },
    // 发布信息模块
    contrastPublish() {
      if (!this.isChangeOrder) return;
      const backupsData = this.backupsData; // 备份数据
      const params = this.getParams(); // 当前数据
      const contrastKey = ['orderLink', 'attachment']; // 需要对比的字段
      const ans = {};
      contrastKey.forEach(i => {
        if (i !== 'attachment' && params[i] !== backupsData[i]) {
          ans[i] = params[i];
        }
        if (i === 'attachment') {
          // 原来有值，被清除了
          const removeAllData = (!params[i] || params[i].length === 0) && backupsData[i] && backupsData[i].length > 0;
          // 原来没值，新增了
          const addNewData = (!backupsData[i] || backupsData[i].length === 0) && params[i] && params[i].length > 0;
          if (removeAllData || addNewData || backupsData[i].length !== params[i].length) {
            ans[i] = params[i];
          } else {
            // 旧的和新的都有值，且长度一致，则需要进行 diff
            let equal = true;
            params[i].forEach((j, index) => {
              if (j.url !== backupsData[i][index].url) {
                equal = false;
              }
            });
            if (!equal) {
              ans[i] = params[i];
            }
          }
        }
      });
      this.contrastPublishData = ans;
    },
    // tool
    diffArr(ans, newVal, oldVal, key, valKey) {
      // 原来有值，被清除了
      const removeAllData = (!newVal[key] || newVal[key].length === 0) && oldVal[key] && oldVal[key].length > 0;
      // 原来没值，新增了
      const addNewData = (!oldVal[key] || oldVal[key].length === 0) && newVal[key] && newVal[key].length > 0;
      const notLength = (oldVal[key] && newVal[key]) && (oldVal[key].length !== newVal[key].length);

      if (removeAllData || addNewData || notLength) {
        ans[key] = newVal[key];
      } else if (newVal[key] && oldVal[key]) {
        const orderKindIds = newVal[key].map(j => j[valKey]);
        const backupsOrderKindIds = oldVal[key].map(j => j[valKey]);
        orderKindIds.sort((a, b) => a - b);
        backupsOrderKindIds.sort((a, b) => a - b);
        if (orderKindIds.join(',') !== backupsOrderKindIds.join(',')) {
          ans[key] = newVal[key];
        }
      }
    }
  }
};
